package com.ajk.server.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.dao.KnowledgeMapper;
import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.model.Knowledge;
import com.ajk.server.model.query.KnowledgeQuery;
import com.ajk.server.model.request.KnowledgeReq;
import com.ajk.server.model.response.KnowledgeRes;
import com.ajk.server.service.KnowledgeService;
import com.ajk.server.utils.FileStoreUtil;

@Service
@Transactional
public class KnowledgeServiceImpl implements KnowledgeService{

	@Autowired
	private KnowledgeMapper knowledgeMapper;
	
	@Override
	public List<KnowledgeRes> list(Knowledge knowledge,Integer start,Integer limit) {
		KnowledgeReq knowledgeReq = null;
		if(knowledge instanceof KnowledgeReq){
			knowledgeReq = (KnowledgeReq) knowledge;
		}
		knowledgeReq.setStart(start);
		knowledgeReq.setLimit(limit);
		List <KnowledgeRes> list = knowledgeMapper.list(knowledgeReq);
		for(KnowledgeRes res : list){
			if(null != res.getPicUrl() && !res.getPicUrl().isEmpty()){
				//res.setPicUrl(FileStoreUtil.getBaseUrl().concat(res.getPicUrl()));
				FileStoreUtil.dealKnowledge(res);
			}
			res.setContent(null);
		}
		return list;
	}

	@Override
	public KnowledgeRes detail(Knowledge knowledge) {
		KnowledgeRes res = knowledgeMapper.detail(knowledge);
		if(res!=null)
			FileStoreUtil.dealKnowledge(res);
		return res;
	}
	
	@Override
	public KnowledgeRes getById(Integer id) {
		KnowledgeRes res = knowledgeMapper.findById(id);
		if(res!=null)
			FileStoreUtil.dealKnowledge(res);
		return res;
	}

	@Override
	public Integer add(Knowledge knowledge) {
		KnowledgeReq knowledgeReq = null;
		if (knowledge instanceof KnowledgeReq) {
			knowledgeReq =(KnowledgeReq) knowledge;
		}
		if(knowledgeReq.getImg() == null || knowledgeReq.getImg().isEmpty()){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "请上传资讯配图,配置规格 640 * 240 ,不能大于512K");
		}
		if(knowledgeReq.getImg()!=null && knowledgeReq.getImg().getSize()>0){
			if (knowledgeReq.getImg().getSize() > 524288) {
				throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "资讯配图不能大于512K");
			}
			String filePath = FileStoreUtil.storeFile(knowledgeReq.getImg(), "knowledge");
			knowledgeReq.setPicUrl(filePath);
		}
		if(knowledgeReq.getTitle() == null || knowledgeReq.getTitle().isEmpty()){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "标题不能为空");
		}
		if(knowledgeReq.getContent() == null || knowledgeReq.getContent().isEmpty()){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "内容不能为空");
		}
		if(knowledgeReq.getPublishTime() == null){
			knowledgeReq.setPublishTime(new Date());
		}
		knowledgeMapper.add(knowledgeReq);
		return knowledgeReq.getId();
	}

	@Override
	public void delete(Integer id) {
		if(id == null){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "文章ID不能为空");
		}
		knowledgeMapper.deleteById(id);
	}

	@Override
	public void update(Knowledge knowledge) {
		KnowledgeReq knowledgeReq = null;
		if (knowledge instanceof KnowledgeReq) {
			knowledgeReq =(KnowledgeReq) knowledge;
		}
		knowledgeReq.setPicUrl(null);
		if(knowledgeReq.getImg()!=null && knowledgeReq.getImg().getSize()>0){
			if (knowledgeReq.getImg().getSize() > 524288) {
				throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "资讯配图不能大于512K");
			}
			String filePath = FileStoreUtil.storeFile(knowledgeReq.getImg(), "knowledge");
			knowledgeReq.setPicUrl(filePath);
		}
		knowledgeMapper.update(knowledgeReq);
	}

	@Override
	public List<Knowledge> findKnowledgeByQuery(KnowledgeQuery query) {
		List<Knowledge> list = knowledgeMapper.findByQuery(query);
		if (CollectionUtils.isNotEmpty(list)) {
			Collections.sort(list,new Comparator<Knowledge>() {

				@Override
				public int compare(Knowledge o1, Knowledge o2) {
					if(o1!=null && o2!=null && o1.getId()!=null && o2.getId()!=null){
						return o2.getId().compareTo(o1.getId());
					}
					return 0;
				}
				
			});
			for (Knowledge knowledge : list) {
				FileStoreUtil.dealKnowledge(knowledge);
			}
		}
		return list;
	}

	@Override
	public Integer countKnowledgeByQuery(KnowledgeQuery query) {
		return knowledgeMapper.countByQuery(query);
	}

}
